package vtc;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.jfree.data.time.*;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RefineryUtilities;

public class DrawDoubleDateVectorQueue extends OutputUnit
{
	private Double[][] events_;
	private Date[] timeTags_;
	private int position_; 
	private int bufferSize_;
	private String chartName_;
	private String xlabel_;
	private String ylabel_;
	private TimeSeriesDemo picture_;
	private int eventVectorSize_;
	
	DrawDoubleDateVectorQueue(String id, VtcArrayBlockingQueue outQueues[], int bufferSize, String chartName, String xlabel, String ylabel, int eventVectorSize)
	{
		super(id, outQueues);
		eventVectorSize_ = eventVectorSize;
		events_ = new Double[bufferSize][eventVectorSize_];
		timeTags_ = new Date[bufferSize];
		position_ = 0;
		bufferSize_ = bufferSize;
		chartName_ = chartName;
		xlabel_ = xlabel;
		ylabel_ = ylabel;
		picture_ = new TimeSeriesDemo(chartName_, xlabel_, ylabel_);
	}
	
	public void outputProcess() throws InterruptedException
	{
		Object event = inValue_.getEvent();
		Date timeTag = inValue_.getTimeTag();
		Object [] eventVector = new Object[eventVectorSize_];
		//System.out.print("TYPE: " + event.getClass().toString() + "\n");
		eventVector = (Object []) event;
		for (int i=0; i < eventVectorSize_; i++)
		{
			events_[position_][i] = (Double)eventVector[i];
		}
		timeTags_[position_] = timeTag;
		position_++;
		if(position_ >= bufferSize_)
		{
			TimeSeriesCollection dataset = new TimeSeriesCollection();
			for(int i=0; i < eventVectorSize_; i++)
			{
				TimeSeries ts = new TimeSeries("Time Serie " + i , Day.class);
				for(int j=0; j<bufferSize_; j++)
				{
					ts.add(new Day(timeTags_[j]), events_[j][i]);
				}
				dataset.addSeries(ts);
			}
			picture_.doPaint(dataset);
			position_ = 0;
		}
	}
}